/*
 * ResourceGuildBuffValue.cpp
 *
 *  Created on: 2014. 8. 1.
 *      Author: mega
 */

#include <algorithm>
#include <Database/Static.h>

#include "ResourceGuildBuffValue.h"

namespace GuildServer { namespace NResource {

ResourceGuildBuffValue::ResourceGuildBuffValue()
{
}

ResourceGuildBuffValue::~ResourceGuildBuffValue()
{
	std::for_each(clsFlyMap.begin(), clsFlyMap.end(), [&](BUFFVALUEMAP::value_type & pair) {
		delete pair.second;
	});
}

void
ResourceGuildBuffValue::Clear()
{
	std::for_each(clsFlyMap.begin(), clsFlyMap.end(), [&](BUFFVALUEMAP::value_type & pair) {
		delete pair.second;
	});
	clsFlyMap.clear();
}


bool
ResourceGuildBuffValue::LoadResource()
{
	Database::ResultSet res = Database::Execute(static_cast<int>(DatabaseType::GUILD_MASTER), "SELECT * FROM resource_guild_buff_value");

	if (0 != res.GetSQLError())
		return false;

	if (0 == res.GetRowCount())
		return false;

	this->Clear();

	for (Database::ResultSet::iterator iter = res.begin(); iter != res.end(); iter++)
	{
		Flyweight * pFly = new Flyweight;
		std::string nationalCode;
		std::string desc;

		nationalCode		= iter->getString("NATIONAL_CODE");

		pFly->skill_tid		= iter->getInt("skill_tid");
		pFly->skill_level	= iter->getInt("skill_level");
		pFly->skill_value	= iter->getInt("skill_value");

		desc				= iter->getString("skill_desc");

		clsFlyMap[Key(pFly->skill_tid, pFly->skill_level)] = pFly;
	}

	return true;
}


ResourceGuildBuffValue::Flyweight *
ResourceGuildBuffValue::GetFly(Key & clsKey)
{
	auto iter = clsFlyMap.find(clsKey);
	if (iter == clsFlyMap.end())
		return NULL;

	return iter->second;
}

} /* namespace NResource */ } /* namespace GuildServer */
